/**
 * jims
 */
package com.jims.master.modules.orders.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.orders.dao.DrugDispRecDao;
import com.jims.master.modules.orders.entity.DrugDispRec;
import com.jims.master.modules.sys.dao.CompanyDao;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 摆药记录Service
 *
 * @author 陈晓阳
 * @version 2017-04-07
 */
@Service
@Transactional(readOnly = true)
public class DrugDispRecService extends CrudService<DrugDispRecDao, DrugDispRec> {
    @Autowired
    private DrugDispRecDao drugDispRecDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private OrgDeptDao orgDeptDao;

    public DrugDispRec get(String id) {
        return super.get(id);
    }

    public List<DrugDispRec> findList(DrugDispRec drugDispRec) {
        return super.findList(drugDispRec);
    }



    public Page<DrugDispRec> findPage(Page<DrugDispRec> page, DrugDispRec drugDispRec) {
        return super.findPage(page, drugDispRec);
    }

    /**
     * 护士端--摆药查询（药品）
     * @param page
     * @param drugDispRec
     * @return
     * @author 赵宁
     */
    public  Page<DrugDispRec> findDrugPage(Page<DrugDispRec> page, DrugDispRec drugDispRec){
        drugDispRec.updateOrgId();
        drugDispRec.setPage(page);
       return page.setList(drugDispRecDao.getPrescDrugClass(drugDispRec));
    }
    /**
     * 护士端--摆药查询（药品）
     * @param drugDispRec
     * @return
     * @author CTQ
     */
    public String prescDurgClassPrint(DrugDispRec drugDispRec){
        String printPath="";
        String hospital = companyDao.get(UserUtils.getOrgId()).getOrgName();
        String wardName = orgDeptDao.get(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"").getDeptName();
        Map map=new HashMap();
        map.put("hospital",hospital);
        map.put("startTime",drugDispRec.getStartTime());
        map.put("endTime",drugDispRec.getEndTime());
        map.put("wardCode",wardName);
        int costs = 0;
        List<BaseDto> list= drugDispRecDao.prescDrugClass(drugDispRec);
        for(int i = 0;i<list.size(); i++){
            costs += list.get(i).getAsInteger("charges");
        }
        map.put("costs",costs);
        map.put("printDate",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        if(list==null || list.size()<0){
            printPath= "暂无数据";
        }else{
            printPath= PrintPdfUtils.previewPdf(UserUtils.getUser().getId(),"his/nurse/DrugClass.jasper",map,list,"drugSummary");
        }
        return printPath;
    }

    /**
     * 护士端--摆药查询（个人）
     * @param page
     * @param drugDispRec
     * @return
     * @author 赵宁
     */
    public Page<DrugDispRec> findDrugPatPage(Page<DrugDispRec> page, DrugDispRec drugDispRec){
        drugDispRec.updateOrgId();
        drugDispRec.setPage(page);
        return page.setList(drugDispRecDao.getPresPatDrug(drugDispRec));
    }

    /**
     * 护士端--摆药查询（个人）-打印
     * @param drugDispRec
     * @return
     * @author CTQ
     */
    public String prescPatDrugPrint(DrugDispRec drugDispRec){
        String printPath="";
        String hospital = companyDao.get(UserUtils.getOrgId()).getOrgName();
        String wardName = orgDeptDao.get(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"").getDeptName();
        Map map=new HashMap();
        map.put("hospital",hospital);
        map.put("startTime",drugDispRec.getStartTime());
        map.put("endTime",drugDispRec.getEndTime());
        map.put("wardCode",wardName);
        int costs = 0;
        List<BaseDto> list= drugDispRecDao.prescPatDrugPrint(drugDispRec);
        for(int i = 0;i<list.size(); i++){
            costs += list.get(i).getAsInteger("charges");
        }
        map.put("costs",costs);
        map.put("printDate",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        if(list==null || list.size()<0){
            printPath= "暂无数据";
        }else{
            printPath= PrintPdfUtils.previewPdf(UserUtils.getUser().getId(),"his/nurse/DrugClass.jasper",map,list,"drugSummary");
        }
        return printPath;
    }

    @Transactional(readOnly = false)
    public int save(DrugDispRec drugDispRec) {
        return super.save(drugDispRec);
    }

    @Transactional(readOnly = false)
    public int delete(DrugDispRec drugDispRec) {
        return super.delete(drugDispRec);
    }

}